package cn.hxzy.teacher.controller;


import cn.hutool.poi.excel.ExcelUtil;
import cn.hxzy.common.domain.Direction;
import cn.hxzy.common.domain.SysSubject;
import cn.hxzy.common.domain.vo.ExcelUtilVo;
import cn.hxzy.common.domain.vo.PageBeanVo;
import cn.hxzy.common.response.R;
import cn.hxzy.common.response.StatusEnum;
import cn.hxzy.teacher.service.ISysSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tyhxzy
 * @since 2024-12-14
 */

@RestController
@RequestMapping("/subject")
public class SysSubjectController {
    @Autowired
    private ISysSubjectService ISysSubjectService;

    // 查询所有
    @RequestMapping("/list")
    public R<PageBeanVo> list(@RequestParam Integer pageNum,//页数
                              @RequestParam Integer pageSize) {//每页显示的条数
        Integer start = (pageNum - 1) * pageSize;
        //分页数据
        List<SysSubject> selectquanbu = ISysSubjectService.selectquanbu(start, pageSize);
        //分页总条数
        Integer count = ISysSubjectService.selectzong();
        PageBeanVo<SysSubject> pageBeanVo = new PageBeanVo();
        pageBeanVo.setTotal(count);
        pageBeanVo.setItems(selectquanbu);
        if (selectquanbu != null) {
            return R.success(pageBeanVo);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }
    }

    @Autowired
    private ISysSubjectService sysSubjectService;

    //新增题目
    @RequestMapping("/addSubject")
    public R addSubject(@RequestBody SysSubject sysSubject) {
        System.out.println(sysSubject);
        Integer i = sysSubjectService.addSubject(sysSubject);
        if (i > 0) {
            return R.success(i);
        }
        return R.error(500, "添加失败");
    }

    //查询题目标签
    @RequestMapping("/selectSubjectLabel")
    public R selectSubjectLabel() {
        return R.success(sysSubjectService.selectSubjectLabel());
    }

    //查询专业名称
    @RequestMapping("/selectSubjectMajor")
    public R selectSubjectMajor() {
        return R.success(sysSubjectService.selectDirection());
    }
    //修改题目
    @RequestMapping("/updateSysSubject")
    public R updateSysSubject(@RequestBody SysSubject sysSubject) {
        Integer i = sysSubjectService.updateSysSubject(sysSubject);
        if (i > 0) {
            return R.success(i);
        }
        return R.error(500, "修改失败");
    }
    //删除题目
    @DeleteMapping("/deleteById/{sid}")
    public R deleteById(@PathVariable Long sid) {
        System.out.println(sid);
        Integer i = sysSubjectService.deleteById(sid);
        if (i > 0) {
            return R.success(i);
        }
        return R.error(500, "删除失败");
    }

    //根据subject_type来查询
    @RequestMapping("/listByType")
    public R<PageBeanVo> listByType(@RequestParam Integer pageNum,//页数
                                    @RequestParam Integer pageSize,//每页显示的条数
                                    @RequestParam Integer subjectType) {
        Integer start = (pageNum - 1) * pageSize;
        List<SysSubject> sysSubjects = ISysSubjectService.selectType(start, pageSize, subjectType);
        Integer count = ISysSubjectService.selecttixing(subjectType);
        PageBeanVo<SysSubject> pageBeanVo = new PageBeanVo();
        pageBeanVo.setTotal(count);
        pageBeanVo.setItems(sysSubjects);

            return R.success(pageBeanVo);

    }

    //根据标签进行查询
    @RequestMapping("/listByLabel")
    public R<PageBeanVo> listByLabel(@RequestBody Map mm) {
        System.out.println("+++---------");
        System.out.println(mm);
        // 提取页码
        Integer pageNum = (Integer) mm.get("pageNum");
        // 提取每页显示的条数
        Integer pageSize = (Integer) mm.get("pageSize");
        // 提取难度
        Integer slv = (Integer) mm.get("sysSubjectLv");
        //提取专业
        Integer zz = (Integer) mm.get("sysSubiectMajor");
        Integer start = (pageNum-1)*pageSize;
        Integer tt=null;
        // 提取题型
        if(mm.get("sysSubjectType")!=null){
            tt =(Integer) mm.get("sysSubjectType");
        }
        // 判断是否指定了标签
        if(mm.get("data")==null){
            //查询没有指定标签的题目
            List<SysSubject> selectxxx = ISysSubjectService.selectxxx(start, pageSize, slv,tt,zz);
            Integer zong = ISysSubjectService.selectxxxzong(slv,tt,zz);
            PageBeanVo<SysSubject> pageBeanVo = new PageBeanVo<>();
            pageBeanVo.setTotal(zong);
            pageBeanVo.setItems(selectxxx);
            return R.success(pageBeanVo);
        }else {
            // 提取 params、pageNum 和 pageSize
            List<Map<String, Object>> dd =(List<Map<String, Object>>) mm.get("data");
            List<SysSubject> allSubjects = new ArrayList<>();
            Integer zong=1;
            // 循环提取标签id
            List<Integer> shu=new ArrayList<>();
            for (Map<String, Object> map : dd){
                Integer labelId = (Integer) map.get("labelId");
                shu.add(labelId);
            }

            //查询标签id,难度,题目类型查询题目
            List<SysSubject> sysSubjects = ISysSubjectService.selectbiaoqian(start, pageSize, shu,slv,tt,zz);
            System.out.println("sysSubjects:"+sysSubjects);
            // 查询标签id,难度,题目类型查询总条数
            Integer count = ISysSubjectService.selectbiaoqianshu(shu,slv,tt,zz);

            PageBeanVo<SysSubject> pageBeanVo = new PageBeanVo<>();
            pageBeanVo.setTotal(count);
            pageBeanVo.setItems(sysSubjects);
            System.out.println("===/====");
            System.out.println(pageBeanVo);
            return R.success(pageBeanVo);
        }
    }

    //查询专业
    @RequestMapping("/major")
    public R<List<Direction>> major(){
        System.out.println("-------=----=----=major");
        List<Direction> major = ISysSubjectService.major();
        if(major==null){
            return R.error(StatusEnum.SERVICE_ERROR);
        }
        return R.success(major);
    }


    //文档导入题目
    @PostMapping("/importData")
    //表示该方法的返回值应直接作为 HTTP 响应的内容，而不是视图名称。
    @ResponseBody
    //用于声明当前方法需要在事务中执行。如果方法执行过程中发生异常，事务会回滚；如果方法正常结束，事务会提交。
    @Transactional
    public R importData(@RequestParam("file") MultipartFile file, @RequestParam("updateSupport") boolean updateSupport) {
        // 判断文件是否为空
        if (file == null || file.isEmpty()) {
            return R.error(StatusEnum.INVALID_FILE_FORMAT); // 使用现有常量替代
        }
        // 判断文件格式
        String originalFilename = file.getOriginalFilename();
        if (!originalFilename.endsWith(".xls") && !originalFilename.endsWith(".xlsx")) {
            return R.error(StatusEnum.INVALID_FILE_FORMAT);
        }

        try {
            System.out.println("文件名：" + originalFilename);
            // 创建ExcelUtilVo对象, 传入SysSubject.class和sysSubjectService
            ExcelUtilVo<SysSubject> excelUtil = new ExcelUtilVo<>(SysSubject.class, sysSubjectService);
            System.out.println("excelUtil:"+excelUtil);
            // 调用导入方法, 传入文件输入流
            List<SysSubject> sysSubjectList = excelUtil.importExcel(file.getInputStream());
            System.out.println("sysSubjectList:"+sysSubjectList);
            // 调用业务逻辑层导入方法, 传入导入的数据和是否更新
            String message = sysSubjectService.importXxxx(sysSubjectList, updateSupport);
            System.out.println("message:"+message);
            return R.success(message);
        } catch (IOException e) {
            return R.error(StatusEnum.IO_EXCEPTION);
        } catch (IllegalArgumentException e) {
            return R.error(StatusEnum.valueOf(e.getMessage()));
        } catch (Exception e) {
            return R.error(StatusEnum.IMPORT_FAILED);
        }
    }
}